VERSION 5.00
Begin VB.UserControl PictureView 
   BackColor       =   &H00FFFFFF&
   BorderStyle     =   1  'Fixed Single
   ClientHeight    =   2790
   ClientLeft      =   0
   ClientTop       =   0
   ClientWidth     =   3690
   ScaleHeight     =   186
   ScaleMode       =   3  'Pixel
   ScaleWidth      =   246
End
Attribute VB_Name = "PictureView"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Const dMargin As Single = 5!
Private mMargin As Single
Private mPicture As IPictureDisp

Public Sub LoadPic(path As String)
    Set mPicture = LoadPicture(path)
    Redraw
End Sub

Public Sub Redraw()
    Dim picWidth    As Single
    Dim picHeight   As Single
    Dim picScale    As Single
    Dim w As Single
    Dim h As Single
    
    Cls
    
    w = ScaleWidth - Margin - Margin
    h = ScaleHeight - Margin - Margin
    
    picWidth = ScaleX(mPicture.Width, vbHimetric, vbPixels)
    picHeight = ScaleY(mPicture.Height, vbHimetric, vbPixels)

    If picHeight < h And picWidth < w Then
        PaintPicture mPicture, (w - picWidth) / 2 + Margin, (h - picHeight) / 2 + Margin, picWidth, picHeight
    ElseIf mPicture.Height / h < mPicture.Width / w Then
        picScale = w / picWidth
        PaintPicture mPicture, Margin, (h - picHeight * picScale) * 0.5 + Margin, w, picScale * picHeight
    Else
        picScale = h / picHeight
        PaintPicture mPicture, (w - picWidth * picScale) * 0.5 + Margin, Margin, picScale * picWidth, h
    End If
End Sub

Public Property Get Margin() As Single
    Margin = mMargin
End Property

Public Property Let Margin(ByVal Value As Single)
    mMargin = Value
    Redraw
End Property

Public Property Get Picture() As IPictureDisp
    Set Picture = mPicture
End Property

Public Property Set Picture(Value As IPictureDisp)
    Set mPicture = Value
    Redraw
End Property


Private Sub UserControl_Initialize()
    mMargin = dMargin
End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    mMargin = PropBag.ReadProperty("Margin", dMargin)
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    PropBag.WriteProperty "Magin", mMargin, dMargin
End Sub
